<?php

function xml_add_method( $methods ) {
	$methods['elliot.phoneHome'] = 'phone_home';
	return $methods;
}
add_filter( 'xmlrpc_methods', 'xml_add_method');

function phone_home( $args ) {
	global $wpdb;
	
	$args = wp_parse_args( $args );
	extract( $args, EXTR_SKIP );
	
	$hash = md5($uid . $plugin_name . $plugin_version . $message);

	// First check to see if this error has been reported by this blog
	if(elliot_unique($hash)) {		
		$query = 'INSERT INTO ' .
				$wpdb->prefix . 'elliot
				( timeStamp, sourceUID, wpVersion, phpVersion, allowUrlFopen, pluginName, pluginVersion, message, hash )
				VALUES (NOW(), %s, %s, %s, %d, %s, %s, %s, %s)';
		$wpdb->query( $wpdb->prepare( $query, $uid, $wp_version, $php_version, $fopen, $plugin_name, $plugin_version, $message, $hash));
	}
	return true;
}

function error_list($type = false, $search = false) {
		$errors = get_errors($type, $search);
		$total_errors = count($errors);
		$style = '';
		foreach($errors as $elliot) {
			$style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
			$error_data=get_single_error($elliot);
			echo "\n\t";
			echo "<tr id='redirect-" . $error_data['id'] . "'" . $style . ">";
			echo '<th scope="row" class="check-column"><input type="checkbox" name="delete_errors[]" id="delete_errors[]" value="' . $error_data['id'] . '" /></th>';
			echo '<td class="column-uid"><a href="'.get_option('siteurl') . '/wp-admin/tools.php?page=elliot/includes/admin.php&type=sourceUID&search=' . $error_data['sourceUID'] . '"><strong>' . $error_data['sourceUID'] . '</strong</a><br />';

			echo '<div class="row-actions">';		
			echo '<span class="delete">';
			echo "<a class=\"submitdelete\" onclick=\"if ( confirm('" . esc_js(sprintf( __("You are about to delete this entry '%s'\n  'Cancel' to stop, 'OK' to delete."), $error_data['id'] )) . "') ) { return true;}return false;\" href=\"?page=elliot/includes/admin.php&amp;action=delete&amp;err_id=" . $error_data["id"] . "\">Delete</a>";
			echo '</span>';			
			echo "</div>";
			
			echo "</td>";
			echo "<td class='column-wp_ver'>" . $error_data['wpVersion'] . "</td>";
			echo "<td class='column-php_ver'>" . $error_data['phpVersion'] . "</td>";
			echo "<td class='column-fopen'>";
			if($error_data['allowUrlFopen']) {
				echo 'On';
			}else{ 
				echo'Off';
			}
			echo "</td>";
			echo "<td class='column-plugin'>" . $error_data['pluginName'] . "</td>";
			echo "<td class='column-version'>" . $error_data['pluginVersion'] . "</td>";
			echo "<td class='column-message'>" . $error_data['message'] . "</td>";
			echo "</tr>";
		}
}

function get_errors($type = false, $search = false) {
     global $wpdb;
     
     if ($type && $search)
     	$errorList = $wpdb->get_col( "SELECT ID FROM " . $wpdb->prefix . "elliot WHERE " . $type . " = '" . $search . "'" );
     else
     	$errorList = $wpdb->get_col( "SELECT ID FROM " . $wpdb->prefix . "elliot" );

     return $errorList;
}

function get_single_error($errid) {
	global $wpdb;
	$query = 'SELECT * FROM ' . $wpdb->prefix . 'elliot WHERE ID=' . $errid;
	$error = $wpdb->get_row($query, ARRAY_A);
	return $error;
}

function delete_error($errid) {
	global $wpdb;
	
	if ( stristr( $errid, ',' ) ) {
		$wpdb->query( "DELETE FROM " . $wpdb->prefix . "elliot WHERE ID IN(" . $errid . ")" );
	} else {
		$wpdb->query( $wpdb->prepare( "DELETE FROM " . $wpdb->prefix . "elliot WHERE ID=%n", $errid ) );
	}
}

function elliot_unique($hash) {
	global $wpdb;
	$hashlist = $wpdb->get_col( "SELECT hash FROM " . $wpdb->prefix . "elliot" );
	if(in_array($hash, $hashlist)) {
		return false;
	} else {
		return true;
	}
}

function process_forms() {

	if ( isset($_POST['dosearch']) )
		wp_redirect(get_option('siteurl') . '/wp-admin/tools.php?page=elliot/includes/admin.php&filter=' . $_POST['filter'] . '&search=' . $_POST['search'] );

	if ( isset($_POST['doaction']) &&  $_POST['action'] !== '-1')
		wp_redirect(get_option('siteurl') . '/wp-admin/tools.php?page=elliot/includes/admin.php&action=' . $_POST['action'] . '&err_id=' . implode( ',', $_POST['delete_errors']) );

}
add_action('admin_init', 'process_forms');
?>